package com.qkjia.oauth2jwtserver.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * Description:添加SpringSecurity配置，允许授权相关路径的访问及表单登录
 *
 * @author JourWon
 * @date 2019/12/23 17:01
 */
@Configuration
@EnableWebSecurity//开启授权
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean//密码编码器
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override//身份验证管理器
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override//配置
    protected void configure(HttpSecurity http) throws Exception {
//        http.csrf().disable();// 默认是启用的，需要禁用CSRF保护
        http.csrf()
                .disable()//禁用
                .authorizeRequests()//authorize 请求
                .antMatchers("/oauth/**", "/login/**", "logout/**")//蚂蚁匹配器
                .permitAll()//允许所有
                .anyRequest()//any请求
                .authenticated()//已认证
                .and()
                .formLogin()//表单登录
                .permitAll();//允许所有
    }
}
